/**
 * @file 辅助相关的一些 reducer
 * @author liaoxuezhi@baidu.com
 */
/* eslint fecs-indent: [0, "space", 2, 2] */


import {
  SET_ASIDE_FOLDED,
  TOGGLE_ASIDE_FOLDED,
  TOGGLE_ASIDE_NAV_OPEN,
  TOGGLE_HEADER_NAV_OPEN,
  SEND_NOTIFY,
  REMOVE_NOTIFY
} from '../util/constants';
import * as storage from '../util/storage';
import {handleActions} from 'redux-actions';
import * as update from 'react-addons-update';

export default handleActions({

  [SET_ASIDE_FOLDED]: (state, action) => update(state, {
    asideFolded: {
      $set: !!action.payload
    }
  }),

  [TOGGLE_ASIDE_FOLDED]: state => update(state, {
    $merge: {
      asideFolded: !state.asideFolded
    }
  }),

  [TOGGLE_ASIDE_NAV_OPEN]: state => update(state, {
    $merge: {
      asideNavOpened: !state.asideNavOpened
    }
  }),

  [TOGGLE_HEADER_NAV_OPEN]: state => update(state, {
    $merge: {
      headerNavOpened: !state.headerNavOpened
    }
  }),

  'ROUTER_CHANGE': state => update(state, {
    $merge: {
      asideNavOpened: false,
      headerNavOpened: false
    }
  }),

  [SEND_NOTIFY]: (state, action) => update(state, {
    notifications: {
      $push: [action.payload]
    }
  }),

  [REMOVE_NOTIFY]: (state, action) => update(state, {
    notifications: {
      $apply: notifications => notifications.filter(item => item.uid !== action.payload)
    }
  }),

  UPDATE_UNREAD_COUNT: (state, action) => update(state, {
    unread_count: {
      $set: action.payload
    }
  })
}, {
  asideFolded: storage.get('asideFolded', false),
  asideNavOpened: storage.get('asideNavOpened', false),
  headerNavOpened: storage.get('headerNavOpened', false),
  notifications: [],
  unread_count: 0
});

